package com.dream.house.service;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import cn.com.servyou.base.exception.ServiceException;
import cn.com.servyou.utils.Page;

import com.dream.house.dao.BuyDAO;
import com.dream.house.dao.RentDAO;
import com.dream.house.dao.ReturnDAO;
import com.dream.house.dao.RoomDAO;
import com.dream.house.entity.RoomDTO;

public class RoomService extends HouseBaseService {
	
	@Autowired
	private RoomDAO roomDAO;
	
	@Autowired
	private BuyDAO buyDAO;
	
	@Autowired
	private RentDAO rentDAO;
	
	@Autowired
	private ReturnDAO returnDAO;
	
	public boolean saveRoom(RoomDTO dto) throws ServiceException{
		dto.setRoom_id(roomDAO.getMaxId());
		return roomDAO.saveRoom(dto);
	}
	
	public boolean modifyRoom(RoomDTO dto) throws ServiceException{
		return roomDAO.updateRoom(dto);
	}
	
	/**
	 * 删除房信息时，需要把房关联的所有信息都删除掉<br>
	 * 1.删除购买记录;<br>
	 * 2.删除租赁记录<br>
	 * 3.删除退房记录<br>
	 * 4.删除房交易信息<br>
	 * 5.删除房信息<br>
	 * @param id
	 * @return boolean
	 * @throws ServiceException
	 */
	public boolean removeRoom(String id) throws ServiceException{
		buyDAO.removeByRoom(id);
		rentDAO.removeByRoom(id);
		returnDAO.removeByRoom(id);
		roomDAO.removeRecord(id);
		return roomDAO.removeRoom(id);
	}
	
	public List<RoomDTO> findRooms(RoomDTO dto) throws ServiceException{
		return roomDAO.findRooms(dto);
	}
	
	public List<RoomDTO> findRoomsWithBuyRent(RoomDTO dto) throws ServiceException{
		List<RoomDTO> dtos=roomDAO.findRoomsWithBuyRent(dto);
		
		
		return roomDAO.findRoomsWithBuyRent(dto);
	}
	
	public Page findByPage(Map<String, Object> condition, int pageNo,
			int pageSize) throws ServiceException{
		
		return roomDAO.findByPage(condition, pageNo, pageSize);
	}
	
	
	public RoomDTO getRoom(String id) throws ServiceException{
		return roomDAO.getRoom(id);
	}
	
	public RoomDTO getRoomDetail(String id) throws ServiceException{
		return roomDAO.getRoomDetail(id);
	}
	
	public boolean grant(String id,String status,String checkUser) throws ServiceException{
		return roomDAO.grant(id, status, checkUser,"1");
	}

	@Override
	public boolean check(Map<String, String> condition) throws ServiceException {
		return roomDAO.check(condition);
	}
}
